package com.storm.queue.thread

import com.storm.queue.core.DelayOperation
import com.storm.queue.model.Job
import com.storm.queue.register.AbstractTopicRegister
import org.slf4j.LoggerFactory
import java.util.concurrent.Executors

object RetryTimeoutThread {
    val log = LoggerFactory.getLogger(this.javaClass)
    /**异步回调 重试2次仍然失败 通知 */
    private val NOTIFY_RETRY_OUT_TIME = Executors.newCachedThreadPool()

    fun callBackExceptionTryRetry(register: AbstractTopicRegister, job: Job, delayOperation: DelayOperation) {
        if (job.retryCount > 1) {
            //重试了2次了,不再重试了; 异步执行回调通知接口;
            NOTIFY_RETRY_OUT_TIME.execute { register.retryOutTimes(job) }
        } else if (job.retryCount >= 0) {
            job.retryCount = job.retryCount + 1
            delayOperation.retryJob(register.getTopic(), job)
            log.warn("失败任务第{}次放入重试:{}:topicId:{},Args:{}", job.retryCount,
                    register.getTopic(), job)
        }
    }

    //停机
    fun toStop() {
        ShutdownThread.closeExecutor(NOTIFY_RETRY_OUT_TIME, "重试任务失败通知线程池")
    }
}